package com.online.taxi.order.implementation.redissonClient;

import com.online.taxi.order.implementation.LockKey;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author:jiangqikun
 * @Date:2022/8/21 14:54
 **/

public class RedissonClientUse {

    @Autowired
    RedissonClient redissonClient;

    public void use(String orderId, String driverId) {
        // 生成 key
        LockKey key = LockKey.builder()
                .driverId(driverId)
                .orderId(orderId)
                .build();
        // 获得锁 此代码默认 设置key 超时时间30秒，过10秒，再延时
        RLock rlock = redissonClient.getLock(orderId.intern());

        try {
            // 此代码默认 设置key 超时时间30秒，过10秒，再延时
            rlock.lock();
            // 执行业务代码


        } finally {
            rlock.unlock();
        }
    }

}
